﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;
using ComponentArt;
public partial class admin_UserControl_MenuAdmin_MenuTopAdminView : System.Web.UI.UserControl
{
    #region "Member variables"
    int intUserID = 0;
    int intModuleID = 0;
    DAChucNang objChucNang;
    DANguoidung objNguoidung;
    bool boolRead ,boolWrite,boolUpdate,boolDelete = false;
    #endregion
    #region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
    #region "Event Method"
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objChucNang = new DAChucNang(sqlConnec);
        objNguoidung = new DANguoidung(sqlConnec);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        loadDataAccess();
        bool boolCheck = false;
        if (Request.QueryString["ChucNang"] != null)
        {
            boolCheck = StringUntil.checkNumber(Request.QueryString["ChucNang"].Trim());
            if (boolCheck)
            {
                intModuleID = Convert.ToInt32(Request.QueryString["ChucNang"].ToString());
            }
        }
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        loadModuleControl();
        loadProfile();
    }
    private void loadProfile()
    {
        this.tbThoat.Tabs.Clear();
        ComponentArt.Web.UI.TabStripTab newItem = new ComponentArt.Web.UI.TabStripTab();
        newItem.Text = Session["Username"].ToString();
        newItem.NavigateUrl = StringUntil.getURLNow(Request.RawUrl.Trim()) + "?ChucNang=-1&UserView=True&UserList=" + intUserID;
        newItem.DefaultSubItemLookId = "DefaultTabSubLook";
        newItem.DefaultSubItemSelectedLookId = "SelectedTabSubLook";
        newItem.DefaultSubGroupAlign = ComponentArt.Web.UI.TabStripAlign.Left;
        this.tbThoat.Tabs.Add(newItem);
        
        newItem = new ComponentArt.Web.UI.TabStripTab();
        newItem.Text = "Thoát";
        newItem.NavigateUrl = StringUntil.getURLNow(Request.RawUrl.Trim()) + "?ChucNang=-1&Logout=True";
        newItem.DefaultSubItemLookId = "DefaultTabSubLook";
        newItem.DefaultSubItemSelectedLookId = "SelectedTabSubLook";
        newItem.DefaultSubGroupAlign = ComponentArt.Web.UI.TabStripAlign.Left;
        this.tbThoat.Tabs.Add(newItem);
    }
    private void loadModuleControl()
    {
        this.tabMenuTopAdmin.Tabs.Clear();
        DataSet dsNguoiDung = objNguoidung.loadNguoiDungChucNang(intUserID);
        DataRow[] drNguoiDungs = dsNguoiDung.Tables[0].Select("ChucNangParent =0 AND Hienthi =1", "ViTri ASC");
        foreach (DataRow childRow in drNguoiDungs)
        {

            ComponentArt.Web.UI.TabStripTab newItem = new ComponentArt.Web.UI.TabStripTab();
            newItem.Text = childRow["TenChucNang"].ToString();
            string strTempChucNang = childRow["TenForm"].ToString().Trim();
            int intCheck = strTempChucNang.ToUpper().IndexOf("HTTP://");
            if (intCheck >= 0)
            {
                newItem.NavigateUrl = childRow["TenForm"].ToString();
                newItem.Target = "_bank";
            }
            else
            {
                newItem.NavigateUrl = StringUntil.getURLNow(Request.RawUrl.Trim()) + "?ChucNang=" + childRow["ID"].ToString() + "&UserList=" + intUserID.ToString();
            }   
            newItem.DefaultSubItemLookId = "DefaultTabSubLook";
            newItem.DefaultSubItemSelectedLookId = "SelectedTabSubLook";
            newItem.DefaultSubGroupAlign = ComponentArt.Web.UI.TabStripAlign.Left;
            loadModuleControlSub(childRow["ID"].ToString(), newItem, dsNguoiDung);
            this.tabMenuTopAdmin.Tabs.Add(newItem);
        }
    }


    private void loadModuleControlSub(string strChucNangeID, ComponentArt.Web.UI.TabStripTab item, DataSet dsNguoiDung)
    {
        DataRow[] drNguoiDungs = dsNguoiDung.Tables[0].Select("ChucNangParent =" + strChucNangeID + " AND Hienthi =1", "ViTri ASC");
        foreach (DataRow childRow in drNguoiDungs)
        {

            ComponentArt.Web.UI.TabStripTab newItem = new ComponentArt.Web.UI.TabStripTab();
            newItem.Text = "&nbsp;&nbsp;" + childRow["TenChucNang"].ToString();
            string strTempChucNang = childRow["TenForm"].ToString().Trim();
            int intCheck = Convert.ToInt32(strTempChucNang.ToUpper().IndexOf("HTTP://"));
            if (intCheck >= 0)
            {
                newItem.NavigateUrl = childRow["TenForm"].ToString();
                newItem.Target = "_bank";
            }
            else
            {
                newItem.NavigateUrl = StringUntil.getURLNow(Request.RawUrl.Trim()) + "?ChucNang=" + childRow["ID"].ToString() + "&UserList=" + intUserID.ToString();
            }   
            newItem.DefaultSubGroupAlign = ComponentArt.Web.UI.TabStripAlign.Left;
            item.Tabs.Add(newItem);
        }
    }
    #endregion
}
